﻿#region MIT License

/*
 * Copyright (c) 2009-2010 Nick Gravelyn (nick@gravelyn.com), Markus Ewald (cygon@nuclex.org)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 * 
 */

#endregion

using System;
using AnimationPackerEngine;

namespace AnimatedSpritePacker
{
    public class CommandLineProgram
    {
        private class ConsoleProgressDisplay : IPackingProgressDisplay
        {
            public void ProgressSectionName(string section)
            {
                Console.Write("\n" + section);
            }

            public void ResetProgress(int max)
            {
            }

            public void Update(int processed)
            {
                do
                {
                    Console.Write('.');
                } while (--processed > 0);
            }

            public void ProgressComplete()
            {
                Console.Write('\n');
            }
        }

        private class PackingStatus : IPackingStatus
        {
            private static void Write(ConsoleColor background, ConsoleColor foreground, string message)
            {
                Console.BackgroundColor = background;
                Console.ForegroundColor = foreground;

                Console.WriteLine(message);
            }

            private static void Write(ConsoleColor foreground, string message)
            {
                Console.ResetColor();
                Console.ForegroundColor = foreground;

                Console.WriteLine(message);
            }

            public void FatalException(string message, Exception e)
            {
                Fatal(message + ": " + MiscHelper.GetFullExceptionMessage(e));
            }

            public void Fatal(string message)
            {
                Write(ConsoleColor.DarkRed, ConsoleColor.Black, message);
            }

            public void Error(string message)
            {
                Write(ConsoleColor.DarkRed, message);
            }

            public void Warning(string message)
            {
                Write(ConsoleColor.DarkGreen, message);
            }

            public void Information(string message)
            {
                Console.ResetColor();
                Console.WriteLine(message);
            }

            public void Debug(string message)
            {
                Write(ConsoleColor.DarkYellow, message);
            }
        }

        static int Main(string[] args)
        {
            ProgramArguments arguments = ProgramArguments.Parse(args);
            if (arguments != null)
            {
                ProcessParams compileParams = new ProcessParams()
                {
                    MaxWidth = arguments.mh,
                    MaxHeight = arguments.mh,
                    Padding = arguments.pad,
                    Pow2 = arguments.pow2,
                    Square = arguments.sqr,
                    SpriteAtlasImage = arguments.imageFile,
                    SpriteAtlasMap = arguments.mapFile,
                    SpriteAtlasIndex = arguments.indexFile
                };

                // compile the final sprite sheet
                if (Worker.CompileAnimations(arguments.input, compileParams,
                                             new ConsoleProgressDisplay(), new PackingStatus()))
                {
                    return 0;
                }
            }

            return -1;
        }
    }
}
